from . import dataop
from dataop import datasource
from dataop import jiandaoyunAPI

datasource = datasource("mysql_155")


# source_jdy.s_jdy_xmxx

@dataop.route('/add/<module_name>/<tablename>', methods=["post", "get"])
def add(module_name, tablename):
    pool = datasource.getdatasource()
    api = jiandaoyunAPI.JianDaoYunAPI()
    # data = api.getTableDatas('劳务结算板块', '项目信息填报表')
    # fields = api.getEntryFields(module_name, tablename)

    tableData = api.getTableDatas(module_name, tablename)

    for row_data in tableData["data"]:
        insert_sql = "insert into source_data_jdy.s_jdy_xmxx ( table_name , data_json) values ( '%s' ,'%s' )" % (
        tablename, repr(tableData).replace("'", '"'))
        datasource.execCustomSql(insert_sql)
        break
    return 'success'


def update(sql):
    pass


def remove(sql):
    pass

# def get_signature(nonce, payload, secret, timestamp):
#     content = ':'.join([nonce, payload, secret, timestamp]).encode('utf-8')
#     m = hashlib.sha1()
#     m.update(content)
#     return m.hexdigest()
#
# @dataop.route('/callback', methods=['POST'])
# def callback():
#     payload = request.data.decode('utf-8')
#     nonce = request.args['nonce']
#     timestamp = request.args['timestamp']
#     if request.headers['x-jdy-signature'] != get_signature(nonce, payload, 'test-secret', timestamp):
#         return 'fail', 401
#     threading.Thread(target=handle, args=(json.loads(payload), )).start()
#     return 'success'
#
# if __name__ == "__main__":
#     add('劳务结算板块', '项目信息填报表')
